package com.corpgovernment.organization.service;

import com.corpgovernment.organization.entity.db.MbOrgEmployeeOpenCardLog;
import com.corpgovernment.organization.mapper.MbOrgEmployeeOpenCardLogMapper;
import com.corpgovernment.supplier.vo.OpenCardDetailListInfoVo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class MbOrgEmployeeOpenCardLogService {

    @Autowired
    private MbOrgEmployeeOpenCardLogMapper mbOrgEmployeeOpenCardLogMapper;

    public MbOrgEmployeeOpenCardLog selectByUid(String uid){
        Example example = new Example(MbOrgEmployeeOpenCardLog.class);
        example.createCriteria().andEqualTo("platUid", uid);
        List<MbOrgEmployeeOpenCardLog> mbOrgEmployeeOpenCardLogs = mbOrgEmployeeOpenCardLogMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(mbOrgEmployeeOpenCardLogs)){
            return mbOrgEmployeeOpenCardLogs.get(0);
        }
        return null;
    }

    public List<MbOrgEmployeeOpenCardLog> getByBatchNo(Long batchNo, Integer failedType, int pageNum, int pageSize, OpenCardDetailListInfoVo openCardDetailListInfoVo){
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(MbOrgEmployeeOpenCardLog.class);
        example.createCriteria().andEqualTo("batchNo", batchNo).andEqualTo("failedType", failedType);
        List<MbOrgEmployeeOpenCardLog> mbOrgEmployeeOpenCardLogs = mbOrgEmployeeOpenCardLogMapper.selectByExample(example);
        PageInfo<MbOrgEmployeeOpenCardLog> pageInfo = new PageInfo<>(mbOrgEmployeeOpenCardLogs);
        if (CollectionUtils.isEmpty(pageInfo.getList())){
            return Collections.emptyList();
        }
        openCardDetailListInfoVo.setCount(pageInfo.getTotal());
        openCardDetailListInfoVo.setPageSize(pageInfo.getPageSize());
        openCardDetailListInfoVo.setTotalPage(pageInfo.getPages());
        openCardDetailListInfoVo.setCurrentPage(pageInfo.getPageNum());
        return pageInfo.getList();
    }

    public int getFailedNum(Long batchNo,Integer failedType){

        Example example = new Example(MbOrgEmployeeOpenCardLog.class);
        example.createCriteria().andEqualTo("batchNo", batchNo).andEqualTo("failedType", failedType);
        return mbOrgEmployeeOpenCardLogMapper.selectCountByExample(example);

    }

    public boolean insert(MbOrgEmployeeOpenCardLog openCardLog){
        if (Objects.isNull(openCardLog)){
            return false;
        }
        return mbOrgEmployeeOpenCardLogMapper.insertSelective(openCardLog) > 0;
    }

}
